package o2o.task.shop;

import com.jinyou.utils.common.DateUtils;
import com.jinyou.utils.common.ValidateUtil;
import com.jinyouapp.common.tools.jms.model.JMSNotify;
import o2o.cache.agent.CityAgentMemory;
import o2o.cache.agent.ShopAgentRelationMemory;
import o2o.cache.shop.ShopMemory;
import o2o.config.dao.DataDaoSql;
import o2o.config.dao.UserDaoSql;
import o2o.config.jms.config.JMSTypeContent;
import o2o.config.jms.sender.JmsSender;
import o2o.dao.PO.PojoTables;
import o2o.dao.PO.agent.CityAgent;
import o2o.company.model.ShopInfo;
import o2o.dao.PO.shop.ShopWorkLog;
import o2o.system.data.SYS_SETTINGS;
import org.apache.commons.lang.StringUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ShopWorkTaskUtils {


    private static List<ShopWorkLog> dataList = new ArrayList<>();

    // 0全时段自动上下班
    public static void autoWorkOnOrOff() {
        List<ShopInfo> shoplist = ShopMemory.getInstance().list();
        // 获取当前时间
        Calendar now = Calendar.getInstance();
        // 小时为24点特殊处理
        int hour = now.get(Calendar.HOUR_OF_DAY);
        if (hour == 0) {
            hour = 24;
        }
        // 分钟为个位数特殊处理
        int mI = now.get(Calendar.MINUTE);
        String minute = "";
        if (mI < 10) {
            minute = "0" + mI;
        } else {
            minute = "" + mI;
        }

        String nowTimeStr = hour + ":" + minute;
        nowTimeStr = nowTimeStr.replace(":", "");
//        System.out.println("此刻" + nowTime);
        if (ValidateUtil.isAbsList(shoplist)) {
            for (int i = 0; i < shoplist.size(); i++) {
                if (null == shoplist.get(i)) {
                    continue;
                }
                // 是否锁定 0:锁定,1:正常
                if (0 != shoplist.get(i).getIsLock() - 1) {
                    continue;
                }
                // 关闭状态 0:关闭,1:正常
                if (0 != shoplist.get(i).getIsOpen() - 1) {
                    continue;
                }
                if (0 == shoplist.get(i).getAutoClose() - 1) {
                    continue;
                }
                Integer isWork = shoplist.get(i).getIsWork(); // 是否上班中 0:已打烊 1:营业中
                Long shopId = shoplist.get(i).getId();   // 店铺Id

                boolean manualBreak = false;
                if (ValidateUtil.isNotNull(shoplist.get(i).getWorktime()) && !shoplist.get(i).getWorktime().equals("0")) {
                    // 某店铺各个时间段
                    String[] workTime = shoplist.get(i).getWorktime().split(",");
                    if (null != workTime && workTime.length > 0) {

                        // 获取最后手动操作时间
                        // 是否在当前时段内，如果是在当前时段内，则不进行操作；
                        // 如果不在当前时段内，则不理，该干啥干啥
                        Long manualWorkTime = shoplist.get(i).getManualWorkTime();
                        Long mauTime = null;
                        if (ValidateUtil.isAbsLong(manualWorkTime)) {
                            // 当前时间换算成时分形式
                            String mauTimeStr = DateUtils.getTimestampHourTime(manualWorkTime);
                            if (ValidateUtil.isNotNull(mauTimeStr)) {
//                                mauTime = Long.parseLong(mauTimeStr.replace(":", "")) == 0 ? 2400 : Long.parseLong(mauTimeStr.replace(":", ""));
//                                mauTime = toLong(mauTimeStr.replace(":", ""));
                                mauTime = timeToLong(mauTimeStr);
                                if (null == mauTime) {
                                    continue;
                                }
                                if (0 == mauTime) {
                                    mauTime = 2400l;
                                }

                            }
                        }


                        Boolean needWork = false;// 需要上班
                        for (int j = 0; j < workTime.length; j++) {
                            String s = workTime[j];
                            if (ValidateUtil.isNull(s) || "null".equalsIgnoreCase(s) || "undefined".equalsIgnoreCase(s)) {
                                continue;
                            }
                            String[] timePoint = s.split("-");
                            if (null == timePoint || timePoint.length < 1) {
                                continue;
                            }
                            //营业时间点（开始结束时间）
//                            Long starTime = (Long.parseLong(timePoint[0].replace(":", ""))) == 0 ? 2400 : Long.parseLong(timePoint[0].replace(":", ""));
//                            Long endTime = (Long.parseLong(timePoint[1].replace(":", ""))) == 0 ? 2400 : Long.parseLong(timePoint[1].replace(":", ""));

//                            Long starTime = toLong(timePoint[0].replace(":", ""));
                            Long starTime = timeToLong(timePoint[0]);
                            if (null == starTime) {
                                continue;
                            }
//                            Long endTime = toLong(timePoint[1].replace(":", ""));
                            Long endTime = timeToLong(timePoint[1]);
                            if (null == endTime) {
                                continue;
                            }
                            if (0 == starTime) {
                                starTime = 2400l;
                            }
                            if (0 == endTime) {
                                endTime = 2400l;
                            }


                            // 开始时间 等于结束时间 表示全天营业
                            if (starTime.longValue() == endTime.longValue()) {
                                // 全天上班  直接上班
                                needWork = true;
                            }
                            if (null == starTime || null == endTime || null == nowTimeStr) {
                                continue;
                            }

                            if (ValidateUtil.isAbsLong(mauTime) && DateUtils.getCurrTime() - manualWorkTime < 24 * 60 * 60 * 1000) {
                                // 大于24小时的手动无效
                                if (starTime <= endTime) {
                                    if (starTime <= Long.parseLong(nowTimeStr) && Long.parseLong(nowTimeStr) <= endTime
                                            && starTime <= mauTime && mauTime <= endTime) {
                                        // 开始时间小于结束时间  例：8:00-9:00
                                        // 在范围内下班 自动上下班不生效了
                                        manualBreak = true;
                                        break;
                                    }

                                } else {// 开始时间大于结束时间  例：24:00-2:00
                                    // 当前时间，分两种情况 24：25  和  1:00
                                    if ((Long.parseLong(nowTimeStr) >= starTime && Long.parseLong(nowTimeStr) >= endTime) || (starTime >= Long.parseLong(nowTimeStr) && endTime >= Long.parseLong(nowTimeStr))) {
                                        if ((mauTime > starTime && mauTime > endTime) || (starTime < mauTime && endTime < mauTime)) {
                                            // 在范围内 自动上下班不生效了
                                            manualBreak = true;
                                            break;
                                        }
                                    }
                                }
                            }

                            // 营业时间在营业范围内则上班
                            if (endTime < starTime) {   // 开始时间大于结束时间  例：24:00-2:00
//                                当前时间 24：25    1:00分两种情况
                                if ((Long.parseLong(nowTimeStr) >= starTime && Long.parseLong(nowTimeStr) >= endTime) || (starTime >= Long.parseLong(nowTimeStr) && endTime >= Long.parseLong(nowTimeStr))) {
                                    // 此刻需营业ing
                                    needWork = true;
                                }

                            } else {  // 开始时间小于结束时间  例：8:00-9:00
                                if (Long.parseLong(nowTimeStr) >= starTime && endTime >= Long.parseLong(nowTimeStr)) {
                                    // 此刻需营业ing
                                    needWork = true;
                                }
                            }
                            if (needWork) {
                                // 初始是需要下班，遇到需要上班的则不用继续循环，直接去上班
                                break;
                            }
                        }


                        if (manualBreak) {
                            // 手动设置了上下班生效中
                            continue;
                        }

                        // 设置为营业
                        if (needWork) {
                            // 店铺需为打烊中
                            if (ValidateUtil.isAbsLong(mauTime) || (null != isWork && 0 != isWork - 1)) {  // 是否营业 0:停业,1:营业
                                StringBuilder sb = new StringBuilder();
                                sb.append("update ").append(PojoTables.ShopInfo).append(" set isWork= 1,manualWorkTime=0")
                                        .append(" where delFlag = 0 and id =").append(shopId);
                                try {
                                    UserDaoSql.getInstance().executeBySql(sb.toString());
                                } catch (SQLException e) {
                                    e.printStackTrace();
                                    continue;
                                }
                                System.out.println(DateUtils.getCurrentDateTime() + " 自动上班:" + sb.toString());
                                //缓存更新
                                ShopMemory.getInstance().modifyById(shopId);
                                try {
                                    JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.SHOP_INFO_UPDATE).setLong01(shopId);
                                    JmsSender.setTopicSender(notify);
                                } catch (Exception e) {
                                }
                                ShopWorkTaskUtils.createShopWorkLog(shoplist.get(i).getId(), 0, 1, 1, 1, 1, "admin");
                            }
                        } else {
                            // 设置为打烊
                            // 店铺需为营业中
                            if (ValidateUtil.isAbsLong(mauTime) || (null != isWork && 0 == isWork - 1)) {
                                StringBuilder sb = new StringBuilder();
                                sb.append("update ").append(PojoTables.ShopInfo).append(" set isWork=0,manualWorkTime=0")
                                        .append(" where delFlag = 0 and id =").append(shopId);
                                try {
                                    UserDaoSql.getInstance().executeBySql(sb.toString());
                                } catch (SQLException e) {
                                    e.printStackTrace();
                                    continue;
                                }
                                System.out.println(DateUtils.getCurrentDateTime() + " 自动下班:" + sb.toString());
                                //缓存更新
                                ShopMemory.getInstance().modifyById(shopId);
                                try {
                                    JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.SHOP_INFO_UPDATE).setLong01(shopId);
                                    JmsSender.setTopicSender(notify);
                                } catch (Exception e) {
                                }
                                ShopWorkTaskUtils.createShopWorkLog(shoplist.get(i).getId(), 1, 0, 1, 1, 1, "admin");
                            }
                        }
                    }
                }
            }
        }
    }

    public static Long timeToLong(String timeStr) {
        if (ValidateUtil.isNull(timeStr)) {
            return null;
        }
        String time = timeStr.replace(":", "");
        if (timeStr.startsWith("0")) {
            // 0点需要特殊处理
            String[] timArr = timeStr.split(":");
            if (null != timArr && timArr.length > 1) {
                String start = timArr[0];
                if (ValidateUtil.isNotNull(start)) {
                    if (!isNumber(start)) {
                        return null;
                    }
                    if (Long.parseLong(start) < 1) {
                        // 0 点  需换成24
                        time = "24" + timArr[1];
                    }
                }
            }
        }
        return toLong(time);
    }

    public static Long toLong(String val) {
        if (ValidateUtil.isNull(val)) {
            return 0l;
        }
        if (!isNumber(val)) {
            return null;
        }
        return Long.parseLong(val);
    }

    public static boolean isNumber(String value) {
        String regEx = "^-?[0-9]+$";
        Pattern pat = Pattern.compile(regEx);
        Matcher mat = pat.matcher(value);
        if (mat.find()) {
            return true;
        } else {
            return false;
        }
    }


    public static void autoWorkOnOrOffV2() {
        List<ShopInfo> shopList = ShopMemory.getInstance().list();
        // 获取当前时间
        Calendar now = Calendar.getInstance();
        // 小时为24点特殊处理
        int hour = now.get(Calendar.HOUR_OF_DAY);
        // 分钟为个位数特殊处理
        int mI = now.get(Calendar.MINUTE);
        String minute = "";
        if (mI < 10) {
            minute = "0" + mI;
        } else {
            minute = "" + mI;
        }
        // 开始时间用这个 modify by yangtk
        String startNowTime = hour + ":" + minute;
        startNowTime = startNowTime.replace(":", "");
        if (hour == 0) {
            hour = 24;
        }
        // 结束时间判断这个
        String nowTime = hour + ":" + minute;
        nowTime = nowTime.replace(":", "");
        if (ValidateUtil.isAbsList(shopList)) {
            for (int i = 0; i < shopList.size(); i++) {
                if (null == shopList.get(i)) {
                    continue;
                }
                // 是否锁定 0:锁定,1:正常
                if (0 != shopList.get(i).getIsLock() - 1) {
                    continue;
                }
                // 关闭状态 0:关闭,1:正常
                if (0 != shopList.get(i).getIsOpen() - 1) {
                    continue;
                }

                // 自动上下班   1. 手动 0 自动
                if (0 == shopList.get(i).getAutoClose() - 1) {
                    continue;
                }

                Integer isWork = shopList.get(i).getIsWork(); // 是否营业 0:停业,1:营业
                Long shopId = shopList.get(i).getId();   // 店铺Id
                if (ValidateUtil.isNotNull(shopList.get(i).getWorktime()) && !shopList.get(i).getWorktime().equals("0")) {
                    // 某店铺各个时间段
                    String[] workTime = shopList.get(i).getWorktime().split(",");
                    if (null != workTime && workTime.length > 0) {
                        boolean needUpdate = false;
                        Boolean needWork = false;//上班状态
                        for (int j = 0; j < workTime.length; j++) {
                            if (null == workTime[j]) {
                                continue;
                            }
                            String s = workTime[j];
                            if (ValidateUtil.isNull(s)) {
                                continue;
                            }
                            String[] timePoint = s.split("-");
                            if (null == timePoint || timePoint.length < 1) {
                                continue;
                            }
                            //营业时间点（开始结束时间）
//                            Long starTime = (Long.parseLong(timePoint[0].replace(":", ""))) == 0 ? 2400 : Long.parseLong(timePoint[0].replace(":", ""));
//                            Long endTime = (Long.parseLong(timePoint[1].replace(":", ""))) == 0 ? 2400 : Long.parseLong(timePoint[1].replace(":", ""));
//                            Long starTime = toLong(timePoint[0].replace(":", ""));
                            Long starTime = timeToLong(timePoint[0]);
                            if (null == starTime) {
                                continue;
                            }
//                            Long endTime = toLong(timePoint[1].replace(":", ""));
                            Long endTime = timeToLong(timePoint[1]);
                            if (null == endTime) {
                                continue;
                            }
                            if (0 == starTime) {
                                starTime = 2400l;
                            }
                            if (0 == endTime) {
                                endTime = 2400l;
                            }
                            if (null == starTime || null == endTime || null == nowTime || startNowTime == null) {
                                continue;
                            }

                            // modify by zhuwx
                            Long sTime = Long.parseLong(startNowTime) - starTime;
                            if (null != sTime && sTime >= 0 && sTime <= 2) {
                                needWork = true;
                                if (0 == 1 - isWork) {//该上班的时间 上班中 就忽略

                                } else {
                                    needUpdate = true;
                                }
                            }
                            Long eTime = Long.parseLong(nowTime) - endTime;
                            if (null != eTime && eTime >= 0 && eTime <= 2) {
                                needWork = false;
                                if (0 == isWork) {//该下班的时间 下班中 就忽略

                                } else {
                                    needUpdate = true;
                                }
                            }

                            if (needUpdate) {
                                break;
                            }
                        }

                        // 设置为营业
                        if (needUpdate) {
                            StringBuilder sb = new StringBuilder();
                            sb.append("update ").append(PojoTables.ShopInfo).append(" set isWork=").append(needWork ? 1 : 0)
                                    .append(",manualWorkTime=0 where delFlag = 0 and id =").append(shopId);
                            try {
                                UserDaoSql.getInstance().executeBySql(sb.toString());
                            } catch (SQLException e) {
                                e.printStackTrace();
                                continue;
                            }
                            System.out.println(DateUtils.getCurrentDateTime() + " V2 自动上下班:" + sb.toString());
                            //缓存更新
                            ShopMemory.getInstance().modifyById(shopId);
                            try {
                                JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.SHOP_INFO_UPDATE).setLong01(shopId);
                                JmsSender.setTopicSender(notify);
                            } catch (Exception e) {
                            }
                            ShopWorkTaskUtils.createShopWorkLog(shopList.get(i).getId(), needWork ? 0 : 1, needWork ? 1 : 0, 1, 1, 1, "admin");
                        }

                    }
                }
            }
        }
    }

    static Integer getLocalTime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMdd");
        return Integer.parseInt(simpleDateFormat.format(new Date()));
    }

    /**
     * 1.0 系统是否在运营范围内
     * a. 如系统未设置 则默认一直在运营
     * b. 如系统设置成0 则一直在运营
     * c. 若系统设置成了 2:00-00:00 则表示系统运营时间是 2:00 -24:00
     * d. 若系统设置成了 18:00-2.00 则表示 今日的18.00以后至 明天的凌晨两点运营
     *
     * @param shopInfo
     * @return
     */
    private static boolean isSystemRunTime(ShopInfo shopInfo) {
        if (shopInfo == null) {
            return false;
        }
        Integer isWork = shopInfo.getIsWork();
        if (isWork == null) {
            isWork = 1;
        }
        String systemRunTime = SYS_SETTINGS.SYSTEM_RUN_TIME;

        boolean isInSystemRunTime = false; // 是否在系统运营范围内
        if (ValidateUtil.isNull(systemRunTime) || systemRunTime.equalsIgnoreCase("0")) {
            isInSystemRunTime = true;
        } else {
            String[] split = StringUtils.split(systemRunTime, "-");
            if (split.length != 2) {
                isInSystemRunTime = true;
            } else {
                String startTimeStr = split[0];
                String endTimeStr = split[1];
                startTimeStr = startTimeStr.replace(":", "");
                endTimeStr = endTimeStr.replace(":", "");
                Integer start = null;
                Integer end = null;
                try {
                    start = Integer.parseInt(startTimeStr);
                    end = Integer.parseInt(endTimeStr);
                } catch (Exception e) {
                    e.printStackTrace();
                    return true;
                }
                if (end == 0) {
                    end = 2400; // 24.00 结束
                }
                Integer local = getLocalTime();
                if (end == start) {
                    // 系统一直运营
                    isInSystemRunTime = true;
                } else if (start > end) { // 开始时间大于结束时间段的话
                    if (local >= start || local <= end) {
                        isInSystemRunTime = true;
                    } else {
                        isInSystemRunTime = false;
                    }
                } else if (end > start) {
                    if (local >= start && local <= end) {
                        isInSystemRunTime = true;
                    } else {
                        isInSystemRunTime = false;
                    }
                } else {
                    // 不在运营范围内
                    isInSystemRunTime = false;
                }
            }
        }
        if (!isInSystemRunTime) {
            if (isWork == 1) {
                StringBuilder sb = new StringBuilder();
                sb.append("update ").append(PojoTables.ShopInfo).append(" set isWork=").append(0)
                        .append(",manualWorkTime=0 where delFlag = 0 and id =").append(shopInfo.getId());
                try {
                    UserDaoSql.getInstance().executeBySql(sb.toString());
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                System.out.println(DateUtils.getCurrentDateTime() + " V3 系统不在运营范围内自动下班:" + sb.toString());
                //缓存更新
                ShopMemory.getInstance().modifyById(shopInfo.getId());
                try {
                    JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.SHOP_INFO_UPDATE).setLong01(shopInfo.getId());
                    JmsSender.setTopicSender(notify);
                } catch (Exception e) {
                }
            } else {
                // 已经下班的就不需要在管了
            }
        }
        return isInSystemRunTime;
    }

    /**
     * 是否在商圈运营范围内
     *
     * @param shopInfo
     * @return
     */
    private static boolean isCityAgentRunTime(ShopInfo shopInfo) {
        if (shopInfo == null) {
            return false;
        }
        Integer isWork = shopInfo.getIsWork();
        if (isWork == null) {
            isWork = 1;
        }
        List<Long> agentIdList = ShopAgentRelationMemory.getInstance().agentListOfShopId(shopInfo.getId());
        if (agentIdList == null || agentIdList.isEmpty()) {
            // 没有商圈的不用管理
            return true;
        }
        CityAgent citA = null;
        Set<Boolean> set = new HashSet<>();
        for (Long agentId : agentIdList) {
            CityAgent cityAgent = CityAgentMemory.getInstance().getById(agentId);
            if (cityAgent == null) {
                continue;
            }
            if (checkValidInRunTime(cityAgent.getOperatingTime())) {
                // 有商圈在经验范围内就不需要关闭店铺
                set.add(true);
                return true;
            } else {
                citA = cityAgent;
                set.add(false);
            }
        }
        if (set.size() == 0) {
            // 没有有效的商圈了
            return true;
        }
        if (!set.isEmpty() && !set.contains(true)) {
            // 都不在运营范围内
            if (isWork == 1) {
                StringBuilder sb = new StringBuilder();
                sb.append("update ").append(PojoTables.ShopInfo).append(" set isWork=").append(0)
                        .append(",manualWorkTime=0 where delFlag = 0 and id =").append(shopInfo.getId());
                try {
                    UserDaoSql.getInstance().executeBySql(sb.toString());
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                System.out.println(DateUtils.getCurrentDateTime() + " V4 商圈不在运营范围内自动下班:" + citA.getOperatingTime() + sb.toString());
                //缓存更新
                ShopMemory.getInstance().modifyById(shopInfo.getId());
                try {
                    JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.SHOP_INFO_UPDATE).setLong01(shopInfo.getId());
                    JmsSender.setTopicSender(notify);
                } catch (Exception e) {
                }
            }
            return false;
        }
        return true;
    }

    // 检测时间是否在指定时间点以内
    private static boolean checkValidInRunTime(String runTime) {
        if (StringUtils.isEmpty(runTime)) {
            return true;
        }
        if (runTime.equals("0:00-0:00")) {
            return true;
        }
        String[] split = StringUtils.split(runTime, "-");
        if (split.length != 2) {
            return true;
        }
        try {
            String start = split[0];
            String end = split[1];
            if (start.equals(end)) {
                // 开始时间等于结束时间
                return true;
            }
            String[] split1 = StringUtils.split(start, ":");
            if (split1.length != 2) {
                return true;
            }
            Integer start_m = Integer.parseInt(split1[1].toString());
            if (start_m < 10) {
                start = split1[0] + "0" + start_m;
            }
            split1 = StringUtils.split(end, ":");
            if (split1.length != 2) {
                return true;
            }
            Integer end_m = Integer.parseInt(split1[1].toString());
            if (end_m < 10) {
                end = split1[0] + "0" + end_m;
            }
            Integer startTime = Integer.parseInt(start.replaceAll(":", ""));
            Integer endTime = Integer.parseInt(end.replaceAll(":", ""));
            // 判断两个时间
            if (startTime == endTime) {
                // 两个时间相等 表示全天的
                return true;
            }
            // 当前时间
            Integer nowTime = Integer.parseInt(new SimpleDateFormat("HHmm").format(new Date()).toString());
            if (endTime == 0) {
                endTime = 2400;
            }
            if (startTime > endTime) {
                // 开始时间大于结束时间 表示跨天的
                if (nowTime <= endTime || nowTime >= startTime) {
                    return true;
                } else {
                    return false;
                }
            } else {
                if (nowTime >= startTime && nowTime <= endTime) {
                    return true;
                } else {
                    return false;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return true;
        }
    }

    // 生产店铺上下班日志
    public static void createShopWorkLog(Long shopId, Integer preWork, Integer afterWork, Integer preLock, Integer afterLock, Integer isSystem, String username) {
        if (preWork == null && afterWork == null    // 未设置工作状态的
                || (preWork == afterWork && ((preLock == null && afterLock == null) || (preLock == afterLock)))) { // 工作状态相同并且锁定状态也相同
            return;
        }
        ShopWorkLog workLog = new ShopWorkLog(shopId, preLock, afterLock, preWork, afterWork, isSystem, username, DateUtils.getCurrTime());
        dataList.add(workLog);
    }

    // 批量插入
    public static void batchShopLog() {
        List<ShopWorkLog> list = new ArrayList<>();
        synchronized (dataList) {
            if (ValidateUtil.isAbsList(dataList)) {
                list.addAll(dataList);
                dataList.clear();
            }
        }
        // list
        if (ValidateUtil.isAbsList(list)) {
            System.out.println("上下班日志~~" + list.size());
            Collections.sort(list, Comparator.comparingLong(ShopWorkLog::getCreateTime));
            Connection conn = null;
            try {
                conn = DataDaoSql.getInstance().getConnection();
                conn.setAutoCommit(false);
            } catch (SQLException e) {
                e.printStackTrace();
                return;
            }
            StringBuffer infoSb = new StringBuffer(" INSERT INTO ").append(PojoTables.SHOP_WORK_LOG)
                    .append(" (shopId,preLock,afterLock,preWork,")   // 5
                    .append("afterWork,isSystem,username,createTime)")  //5
                    .append("values(?,?,?,?,")
                    .append("?,?,?,?)");
            PreparedStatement infoPre = null;
            try {
                infoPre = conn.prepareStatement(infoSb.toString());
            } catch (Exception e) {
                e.printStackTrace();
                DataDaoSql.getInstance().rollback(conn);
                DataDaoSql.getInstance().close(conn);
                return;
            }

            try {
                for (ShopWorkLog info : list) {
                    infoPre.setLong(1, info.getShopId());
                    infoPre.setInt(2, info.getPreLock());
                    infoPre.setInt(3, info.getAfterLock());
                    infoPre.setInt(4, info.getPreWork());
                    infoPre.setInt(5, info.getAfterWork());
                    infoPre.setInt(6, info.getIsSystem());
                    infoPre.setString(7, info.getUsername());
                    infoPre.setLong(8, info.getCreateTime());
                    infoPre.addBatch();
                }
                // 添加
                infoPre.executeBatch();
            } catch (Exception e) {
                e.printStackTrace();
                DataDaoSql.getInstance().rollback(conn);
                DataDaoSql.getInstance().close(conn);
                return;
            }
            try {
                conn.commit();
            } catch (Exception e) {
                e.printStackTrace();
                DataDaoSql.getInstance().rollback(conn);
            } finally {
                DataDaoSql.getInstance().close(conn);
            }
        }
    }


}
